﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{
    [LanguageSupport]
    [Authorize(Roles = "Admin")]
    public class ManageGalleriesController : BaseController
    {
        public IGalleriesRepository GalleriesRepository { get; set; }
        public override ILanguagesRepository LanguagesRepository { get; set; }

        public ManageGalleriesController(IGalleriesRepository galleriesRepository, ILanguagesRepository languagesRepository)
        {
            GalleriesRepository = galleriesRepository;
            LanguagesRepository = languagesRepository;
        }

        //
        // GET: /Galleries/

        public ViewResult Index()
        {
            var galleries = GalleriesRepository.FindAll();
            return View(galleries);
        }

        //
        // GET: /Galleries/Details/5

        public ViewResult Details(Int32 id)
        {
            var gallery = GalleriesRepository.GetById(id);
            return View(gallery);
        }

        //
        // GET: /Galleries/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Galleries/Create

        [HttpPost]
        public ActionResult Create(Gallery gallery)
        {
            if (ModelState.IsValid)
            {
                gallery.Links = NormalizeLinks(gallery.Links);
                GalleriesRepository.Insert(gallery);
                GalleriesRepository.Save();
                return RedirectToAction("Index");
            }

            return View(gallery);
        }

        //
        // GET: /Galleries/Edit/5

        public ActionResult Edit(int id)
        {
            var gallery = GalleriesRepository.GetById(id);
            return View(gallery);
        }

        //
        // POST: /Galleries/Edit/5

        [HttpPost]
        public ActionResult Edit(Gallery gallery)
        {
            var dbGallery = GalleriesRepository.GetById(gallery.Id);
            if (ModelState.IsValid)
            {
                UpdateModel(dbGallery);
                dbGallery.Links = NormalizeLinks(dbGallery.Links);
                GalleriesRepository.Save();
                return RedirectToAction("Index");
            }
            return View(gallery);
        }

        //
        // GET: /Galleries/Delete/5

        public ActionResult Delete(int id)
        {
            var gallery = GalleriesRepository.GetById(id);
            return View(gallery);
        }

        //
        // POST: /Galleries/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var gallery = GalleriesRepository.GetById(id);
            GalleriesRepository.Delete(gallery);
            GalleriesRepository.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            GalleriesRepository.Dispose();
            base.Dispose(disposing);
        }

        private String NormalizeLinks(String linksString)
        {
            var links = linksString.Split(new[] {',', ' ', '\t', '\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
            return String.Join(",\r\n", links);
        }
    }
}